//
//  UIAlertController+Show.m
//  industry
//
//  Created by 王雅强 on 2021/2/26.
//  Copyright © 2021 Haier. All rights reserved.
//

#import "UIAlertController+Show.h"

@implementation UIAlertController (Show)


- (void)show{
    [self present:true completion:nil];
}

- (void)present:(BOOL)animated completion:(void(^)(void))completion{
    
    UIViewController * vc = [[UIApplication sharedApplication].keyWindow rootViewController];
    if (vc) {
        [self presentFromController:vc andAnimated:animated completion:completion];
    }
}

- (void)presentFromController:(UIViewController *)controller andAnimated:(BOOL)animated completion:(void(^)(void))completion{
    UIViewController * vc = [self topViewController:controller];
    
    if (vc) {
        [vc presentViewController:self animated:animated completion:completion];
    }
    
}

- (UIViewController *)topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self topViewController:[(UINavigationController *)vc visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self topViewController:vc.presentedViewController];
        } else {
            return vc;
        }
    }
    return nil;
}


@end
